package com.magicsoft.share.ui.exchange;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.exchange.GoodsFragmentAdapter;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.exchange.GoodsType;
import com.magicsoft.share.bean.share.Banner;
import com.magicsoft.share.presenter.exchange.ExchangePresenter;
import com.magicsoft.share.ui.main.H5Activity;
import com.magicsoft.share.utils.AppBarStateChangeListener;
import com.magicsoft.share.view.exchange.IExchangeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExchangeFragment extends BaseFragment<IExchangeView, ExchangePresenter> implements IExchangeView {


    @BindView(R.id.recyclerView_goods)
    RecyclerView recyclerViewGoods;
    @BindView(R.id.btn_shop_cart)
    ImageButton btnShopCart;

    @BindView(R.id.view)
    View view;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.banner)
    MZBannerView banner;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPage)
    ViewPager viewPage;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    private GoodsFragmentAdapter goodsFragmentAdapter;
    private ArrayList<Banner> arrayList;
    private int selectedTabPosition;

    private boolean isFirst=true;
    private List<Banner> mBanners;

    public ExchangeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();//暂停轮播
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.d("lyf", "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
        onResume();
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.d("lyf", "onResume");
        banner.start();//开始轮播
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_exchange;
    }

    @Override
    public ExchangePresenter initPresenter() {
        return new ExchangePresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initView() {

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {



            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isFirst=false;

                requestData();
                selectedTabPosition = tabLayout.getSelectedTabPosition();
            }
        });


        initBanner();

        initTab();


        appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.d("STATE", state.name());
                if (state == State.EXPANDED) {
                    //展开状态
                    view.setVisibility(View.GONE);

                } else if (state == State.COLLAPSED) {
                    //折叠状态
                 //   view.setVisibility(View.VISIBLE);

                } else {

                    //中间状态
                 //   view.setVisibility(View.VISIBLE);

                }
            }
        });


    }

    private void initTab() {


        goodsFragmentAdapter = new GoodsFragmentAdapter(getChildFragmentManager());
        viewPage.setAdapter(goodsFragmentAdapter);
        tabLayout.setupWithViewPager(viewPage);
    }


    @Override
    protected void requestData() {

        mPresenter.bannerList();
        mPresenter.goodsTypeList();
    }

    private void initBanner() {

        banner.setIndicatorVisible(false);
        banner.setDelayedTime(3000);

        this.banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {

                Banner ban = mBanners.get(i);
                if (ban.getContentType()==2){
                    Intent intent = new Intent(getContext(), H5Activity.class);
                    intent.putExtra("urlStr",ban.getLink());
                    intent.putExtra("titleStr",ban.getTitle());
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(getContext(), GoodsActivity.class);
                    intent.putExtra("goodsId",ban.getRelationId());
                    startActivity(intent);
                }
            }
        });

    }


    @OnClick(R.id.btn_shop_cart)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), ShopCartActivity.class));
    }

    @Override
    public void bannerListSuccess(List<Banner> banners) {
        // 设置数据
       mBanners=banners;

        banner.setPages(banners, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        banner.start();//开始轮播


    }

    @Override
    public void goodsTypeListSuccess(List<GoodsType> goodsTypes) {
        if (isFirst){
            goodsFragmentAdapter.setData(goodsTypes);
        }else {
            refreshLayout.finishRefresh();
            goodsFragmentAdapter.refreshData(goodsTypes,selectedTabPosition);
            // tabLayout.getTabAt(selectedTabPosition).select();
        }


    }


    public static class BannerViewHolder implements MZViewHolder<Banner> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.item_view_card, null);
            mImageView = (ImageView) view.findViewById(R.id.imageView);
            return view;
        }

        @Override
        public void onBind(Context context, int position, Banner data) {
            // 数据绑定
            Glide.with(context).load(data.getImg()).into(mImageView);
        }

    }

}
